/**
 * 
 */
package com.zte.claa.inficombo.csa.app.service.rptparse.ext.gnss.mina;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zte.claa.inficombo.csa.app.service.rptparse.ext.gnss.mina.msgbean.GnssRequest;

/**
 * @author 0283000115
 *
 */
public class GnssMsgEncoder implements ProtocolEncoder {
	private static final Logger LOG = LoggerFactory.getLogger(GnssMsgEncoder.class);
	// 字符集
	private Charset charset = null;
	// private CharsetEncoder charsetEncoder = null;

	public GnssMsgEncoder(Charset charset) {
		this.charset = charset;
		if (this.charset == null) {
			this.charset = Charset.forName("UTF-8");
		}
		// this.charsetEncoder = this.charset.newEncoder();
	}

	@Override
	public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception {
		LOG.info("Begin encode message for session: " + session.getId()+", msg: "+msg);
		GnssRequest message = (GnssRequest)msg;
		IoBuffer buf = IoBuffer.allocate(message.getPayLoadSize(), false).setAutoExpand(true);
		buf.put(message.getPayLoad());
		// 整理buf，准备输出数据
		buf.flip();
		LOG.info("Finish encoding for session: " + session.getId());
		out.write(buf);
		buf.free();
		buf = null;
	}

	@Override
	public void dispose(IoSession session) throws Exception {
		
	}

}
